package main

import (
	"fmt"
	"reflect"
	"strings"
)

type mystruct struct {
	Name string
	Sex  int
	Age  int `json:"age" valid:"1-100"`
}

func main() {
	var map1 map[string]string
	var map2 = make(map[string]string, 0)
	fmt.Println("map1 is nil ", map1 == nil)
	fmt.Println("map2 is nil", map2 == nil)
	fmt.Println(reflect.TypeOf(map2))
	fmt.Println(reflect.TypeOf(map2).Kind())

	typeofmystruct := reflect.TypeOf(mystruct{})
	fmt.Println(typeofmystruct)
	fmt.Println(typeofmystruct.Name()) //获取反射类型对象  mystruct

	fmt.Println("kind:", typeofmystruct.Kind()) //获取反射类型种类  struct

	fmt.Println(reflect.TypeOf(&mystruct{}))
	fmt.Println(reflect.TypeOf(&mystruct{}).Elem())        // 获取指针类型指向的元素类型的名称
	fmt.Println(reflect.TypeOf(&mystruct{}).Elem().Name()) // 获取指针类型指向的元素类型的名称
	fmt.Println(reflect.TypeOf(&mystruct{}).Elem().Kind()) // 获取指针类型指向的元素类型的种类

	fmt.Println("NumField:", reflect.TypeOf(mystruct{}).NumField())
	for i := 0; i < reflect.TypeOf(mystruct{}).NumField(); i++ {
		fieldtype := reflect.TypeOf(mystruct{}).Field(i)
		fmt.Println("fieldtype:", fieldtype)
		fmt.Println("fieldtype-kind:", reflect.TypeOf(fieldtype).Kind())
		fmt.Println(fieldtype.Name, fieldtype.Type)
		fmt.Println(reflect.TypeOf(mystruct{}).FieldByName(fieldtype.Name))
	}
	var arr = strings.Split((string(reflect.TypeOf(mystruct{}).Field(2).Tag)), " ")
	fmt.Println(reflect.TypeOf(arr).Kind(), arr, len(arr))
	var map3 = map[string]string{}
	for i := 0; i < len(arr); i++ {
		var obj = strings.Split(arr[i], ":")
		map3[obj[0]] = obj[1]
	}
	var valid1 = strings.Replace(map3["valid"], "\"", "", 2)
	fmt.Println(valid1)
	var valid2 = reflect.TypeOf(mystruct{}).Field(2).Tag.Get("valid") // Tag.Get
	fmt.Println(valid2)
	fmt.Println(reflect.TypeOf(valid1), valid1 == valid2)
}
